package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.reservationserver.dao.MembersDao;
import com.ruoyi.reservationserver.entity.Members;
import com.ruoyi.reservationserver.service.MembersService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员表(Members)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:49
 */
@Service("membersService")
public class MembersServiceImpl extends ServiceImpl<MembersDao, Members> implements MembersService {
    @Resource
    private MembersDao membersDao;

    /**
     * 通过ID查询单条数据
     *
     * @param memberId 主键
     * @return 实例对象
     */
    @Override
    public Members queryById(Integer memberId) {
        return this.membersDao.queryById(memberId);
    }

    /**
     * 分页查询
     *
     * @param members 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Members> queryByPage(Members members, PageRequest pageRequest) {
        long total = this.membersDao.count(members);
        return new PageImpl<>(this.membersDao.queryAllByLimit(members, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param members 实例对象
     * @return 实例对象
     */
    @Override
    public Members insert(Members members) {
        this.membersDao.insert(members);
        return members;
    }

    @Override
    public Members insertMember(Integer userId){
        Members members =new Members();
        members.setUserId(userId);
        members.setLevelId(1);
        members.setStatus(0);
        this.membersDao.insert(members);

        return members;
    }

    /**
     * 修改数据
     *
     * @param members 实例对象
     * @return 实例对象
     */
    @Override
    public Members update(Members members) {
        this.membersDao.update(members);
        return this.queryById(members.getMemberId());
    }

    /**
     * 通过主键删除数据
     *
     * @param memberId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer memberId) {
        return this.membersDao.deleteById(memberId) > 0;
    }
}
